<template>
	<view>
		<!-- <button type="primary" @click="userLogin">测试登录</button> -->

		<button open-type="getPhoneNumber" @getphonenumber="userLogin" plain="true">获取手机号</button>
	</view>
</template>
<script lang="ts" setup>
	// import WXBizDataCrypt from "../../utils/WXBizDataCrypt"
	import { ref } from "vue"
	import type { UserPhoneType } from "@/type/login"
	let openId = ref(null)
	let session_key = ref(null)
	let unionid = ref(null)
	const getUserPhone = (param : UserPhoneType) => {
		const { code } = param
		uni.request({
			url: 'https://api.weixin.qq.com/sns/jscode2session',
			method: 'GET',
			data: {
				appid: 'wx23a3ec0e2835e995', //你的小程序的APPID  
				secret: '222272e92a51fd882a5598d92e4122dd', //你的小程序的secret,  
				js_code: code,
				grant_type: 'authorization_code',

			},
			success: (res : any) => {
				console.log(res, 'res')
				openId.value = res.data.openid //openid 用户唯一标识  
				unionid.value = res.data.unionid //unionid 开放平台唯一标识   当公众号和小程序同时登录过才会有
				session_key.value = res.data.session_key //session_key  会话密钥   
				try {
					console.log("---",res.data.session_key)
					console.log("0",window)
					
				} catch (e) {
					//TODO handle the exception
				}

			}
		})
	}
	const getPhoneNumber = () => {

	}
	const userLogin = () => {
		uni.getUserInfo({
			desc: "小程序登录",
			success: ({ userInfo }) => {
				// 微信官方默认返回用户头像和名称
				console.log(userInfo, 'userInfo')
				uni.login({
					provider: 'univerify',
					success: loginRes => {
						const { code,authResult } = loginRes
						console.log(code,authResult, 'code')
						getUserPhone({ code })
					}
				})
			},
		});
	};
</script>